package main

import (
	zimgio "wisdom_education_in_kunshan/image"
	"wisdom_education_in_kunshan/image/threshold"
	"log"
)

func main() {
	showGray("E:/gopath/src/gitee.com/zedongzzd/wisdom_education_in_kunshan/downcode/tmp/src/0838_2318.png")
}

func showGray(imgPath string) {
	i, err := zimgio.Read(imgPath)
	if err != nil {
		log.Println(err)
	}
	pi := zimgio.New(i.Width, i.Height, nil)
	pi.Copy(0, 0, i, 0, 0, i.Width, i.Height)
	ri := zimgio.New(i.Width, i.Height, nil)
	ri.Copy(0, 0, i, 0, 0, i.Width, i.Height)
	//
	tImage := threshold.TImage{pi, nil}
	t := tImage.ByOSTU()
	tImage.Binaryzation(t)
	//
	tImage2 := threshold.TImage{ri, nil}
	t2 := tImage2.ByOSTU()
	tImage2.Binaryzation(t2)
	res := dAcnode(ri)
	/////////////
	result := zimgio.New(i.Width, i.Height*3, nil)
	result.Copy(0, 0, i, 0, 0, i.Width, i.Height)
	result.Copy(0, i.Height, pi, 0, 0, i.Width, i.Height)
	result.Copy(0, i.Height*2, res, 0, 0, i.Width, i.Height)
	result.SaveAsPNG("E:/gopath/src/gitee.com/zedongzzd/wisdom_education_in_kunshan/downcode/tmp/src/res.png")
}

func dAcnode(img *zimgio.Image) *zimgio.Image {
	res := zimgio.Clone(img)
	for x := 0; x < img.Width; x++ {
		for y := 0; y < img.Height; y++ {
			num, direction := findEdgeNum(img, x, y)
			if img.RGBA[x][y].R == 0 {
				if num < 2 {
					res.RGBA[x][y].R = 0xFF
					res.RGBA[x][y].G = 0xFF
					res.RGBA[x][y].B = 0xFF
				}
			} else {
				switch findSEdgeNum(img, x, y) {
				case 3:
					direction |= 0x10101010
					if getDirectionFFNum(img, x, y, direction) <= 4 {
						res.RGBA[x][y].R = 0
						res.RGBA[x][y].G = 0
						res.RGBA[x][y].B = 0
					}
					// if findIEdgeNum(img, x, y) >= 2 {
					// 	res.RGBA[x][y].R = 0
					// 	res.RGBA[x][y].G = 0
					// 	res.RGBA[x][y].B = 0
					// }
				case 4:
					res.RGBA[x][y].R = 0
					res.RGBA[x][y].G = 0
					res.RGBA[x][y].B = 0
				}

			}
		}
	}
	return res
}

// findHEdgeNum 上下左右
func findSEdgeNum(img *zimgio.Image, x, y int) int {
	w := img.Width
	h := img.Height
	num := 0
	if x > 1 && img.RGBA[x-1][y].R == 0 {
		num++
	}
	if x < w-1 && img.RGBA[x+1][y].R == 0 {
		num++
	}
	if y > 1 && img.RGBA[x][y-1].R == 0 {
		num++
	}
	if y < h-1 && img.RGBA[x][y+1].R == 0 {
		num++
	}
	return num
}

func getXY(x, y int, d int) (int, int) {
	if d&0x10000000 == 0x10000000 {
		return x - 1, y - 1
	}
	if d&0x01000000 == 0x01000000 {
		return x, y - 1
	}
	if d&0x00100000 == 0x00100000 {
		return x + 1, y - 1
	}
	if d&0x00010000 == 0x00010000 {
		return x + 1, y
	}
	if d&0x00001000 == 0x00001000 {
		return x + 1, y + 1
	}
	if d&0x00000100 == 0x00000100 {
		return x, y + 1
	}
	if d&0x00000010 == 0x00000010 {
		return x - 1, y + 1
	}
	if d&0x00000001 == 0x00000001 {
		return x, y + 1
	}
	return x, y
}

func getDirectionFFNum(img *zimgio.Image, x, y, direction int) int {
	w := img.Width
	h := img.Height
	x, y = getXY(x, y, direction)
	if x >= 0 && x < w && y >= 0 && y < h {
		if img.RGBA[x][y].R == 0xFF {
			return 1 + getDirectionFFNum(img, x, y, direction)
		} else {
			return 0
		}
	}
	return 10
}

// findIEdgeNum 斜方向
func findXEdgeNum(img *zimgio.Image, x int, y int) int {
	w := img.Width
	h := img.Height
	num := 0
	if x > 1 && y > 1 && img.RGBA[x-1][y-1].R == 0 {
		num++
	}
	if x > 1 && y < h-1 && img.RGBA[x-1][y+1].R == 0 {
		num++
	}
	if x < w-1 && y > 1 && img.RGBA[x+1][y-1].R == 0 {
		num++
	}
	if x < w-1 && y < h-1 && img.RGBA[x+1][y+1].R == 0 {
		num++
	}
	return num
}

func findEdgeNum(img *zimgio.Image, x int, y int) (int, int) {
	w := img.Width
	h := img.Height
	num := 0
	direction := 0x00000000
	if x > 1 && y > 1 && img.RGBA[x-1][y-1].R == 0 {
		num++
		direction |= 0x10000000
	}
	if y > 1 && img.RGBA[x][y-1].R == 0 {
		num++
		direction |= 0x01000000
	}
	if x < w-1 && y > 1 && img.RGBA[x+1][y-1].R == 0 {
		num++
		direction |= 0x00100000
	}
	if x < w-1 && img.RGBA[x+1][y].R == 0 {
		num++
		direction |= 0x00010000
	}
	if x < w-1 && y < h-1 && img.RGBA[x+1][y+1].R == 0 {
		num++
		direction |= 0x00001000
	}
	if y < h-1 && img.RGBA[x][y+1].R == 0 {
		num++
		direction |= 0x00000100
	}
	if x > 1 && y < h-1 && img.RGBA[x-1][y+1].R == 0 {
		num++
		direction |= 0x00000010
	}
	if x > 1 && img.RGBA[x-1][y].R == 0 {
		num++
		direction |= 0x00000001
	}
	return num, direction
}
